# for some reasons, you should install this packages at first
options(encoding = 'UTF-8')
options("repos" = c(CRAN="https://mirrors.tuna.tsinghua.edu.cn/CRAN/"))
options(BioC_mirror="https://mirrors.tuna.tsinghua.edu.cn/bioconductor")


if (!requireNamespace("withr", quietly = TRUE)){
  install.packages("withr", dependencies=TRUE, INSTALL_opts = c('--no-lock'))
}

if (!requireNamespace("xml2", quietly = TRUE)){
  install.packages("xml2", dependencies=TRUE, INSTALL_opts = c('--no-lock'))
}

if (!requireNamespace("languageserver", quietly = TRUE)){
  install.packages("languageserver", dependencies=TRUE, INSTALL_opts = c('--no-lock'))
}

if (!requireNamespace("rJava", quietly = TRUE)){
  install.packages("rJava", dependencies=TRUE, INSTALL_opts = c('--no-lock'))
}
if (!requireNamespace("lintr", quietly = TRUE)){
  install.packages("lintr", dependencies=TRUE, INSTALL_opts = c('--no-lock'))
}

if (!requireNamespace("haven", quietly = TRUE)){
  withr::with_makevars(c(PKG_LIBS = "-liconv"), install.packages("haven"), assignment = "+=")
}

if (!requireNamespace("readxl", quietly = TRUE)){
  withr::with_makevars(c(PKG_LIBS = "-liconv"), install.packages("readxl"), assignment = "+=")
}

if (!requireNamespace("tidyverse", quietly = TRUE)){
  install.packages('tidyverse')
}

# %%
require_packages = c(
  "AlgDesign",
  "ape",
  "arules",
  "BayesTree",
  "Ball", # for debug
  "bmp",
  "Cairo",
  "chron",
  "crayon",
  "data.table",
  "DBI",
  "devtools",
  "downloader",
  "dplyr",
  "DT",
  "e1071",
  "emdbook",
  "evd",
  "expm",
  "fields",
  "fmsb",
  "foreign",
  "formatR",
  "formattable",
  "fortunes",
  "gafit",
  "gcookbook",
  "getopt",
  "GGally",
  "ggiraph",
  "ggmap",
  "ggplot2",
  "glmnet",
  "googleVis",
  "gridExtra",
  "gsubfn",
  "gtable",
  "gvlma",
  "Hmisc",
  "htmltools",
  "IDPmisc",
  "igraph",
  "IRdisplay",
  "jpeg",
  "kableExtra",
  "knitr",
  "kohonen",
  "lars",
  "lattice",
  "lubridate",
  "magic",
  "magrittr",
  "MASS",
  "mcmc",
  "mice",
  "miniUI",
  "mosaic",
  "mvnormtest",
  "nnet",
  "nortest",
  "officer",
  "openxlsx",
  "optparse",
  "outliers",
  "pheatmap",
  "pixmap",
  "plotrix",
  "plyr",
  "png",
  "pool",
  "pracma",
  "pROC",
  "psych",
  "quantmod",
  "R.matlab",
  "R.utils",
  "R6",
  "Rcpp",
  "repr",
  "readxl",
  "reshape2",
  "RgoogleMaps",
  "rhandsontable",
  "rjson",
  "rmarkdown",
  "rms",
  "robust",
  "robustbase",
  "rsconnect",
  "Rserve",
  "RSQLite",
  "rstudioapi",
  "rticles",
  "RUnit",
  "rvest",
  "rworldmap",
  "scatterplot3d",
  "sciplot",
  "servr",
  "shiny",
  "shinyAce",
  "shinyBS",
  "shinycssloaders",
  "shinydashboard",
  "shinyjs",
  "shinythemes",
  "sjmisc",
  "sos",
  "sound",
  "spam",
  "sqldf",
  "stringi",
  "stringr",
  "treemapify",
  "vars",
  "vcd",
  "wordcloud",
  "xlsx",
  "XML",
  "xts",
  "zip",
  "zoo",
  "car",
  "microbenchmark",
  "RMySQL",
  "RODBC"
)

install_packages = setdiff(require_packages,unname(installed.packages()[,1]))
print(install_packages)
if(length(install_packages)){install.packages(install_packages)}

# %%
bioPackages = c(
    "R.utils", "data.table",
    "BioBase",
    "NMF",
    "maftools",
    "GEOquery",
    "FactoMineR", "factoextra", "ggfortify",
    "pheatmap",
    "ggplot2",
    "limma", "DESeq2", "edgeR",
    "clusterProfiler", "org.Hs.eg.db", "org.Mm.eg.db",
    "pathview",
    "sigminer"
)
    # "GDCRNATools",
    # "RTCGA", "RTCGA.rnaseq", "RTCGA.clinical", "RTCGA.mutations",
    # "RTCGA.mRNA", "RTCGA.miRNASeq", "RTCGA.RPPA", "RTCGA.CNV", "RTCGA.methylation"

CRANpackages <- row.names(available.packages())
install_packages = setdiff(bioPackages, unname(installed.packages()[,1]))

print(install_packages)

lapply(install_packages, function(bioPackage){
         if (!require(bioPackage, character.only = T)){
           if (bioPackage %in% CRANpackages){
             install.packages(bioPackage)
           }else{
             if (!requireNamespace("BiocManager", quietly = TRUE))
               install.packages("BiocManager")
             BiocManager::install(bioPackage, update = TRUE, ask = FALSE)
           }
         }
})

# %%
# install from github
library(devtools)
devtools::install_github("rstudio/addinexamples")
devtools::install_github("mkuhn/dict")
devtools::install_github("thomasp85/ggforce")
devtools::install_github("yihui/servr")
devtools::install_github("hafen/rmote")
devtools::install_github("GuangchuangYu/DOSE")
devtools::install_github("GuangchuangYu/enrichplot")
devtools::install_github("GuangchuangYu/clusterProfiler")
devtools::install_github("nx10/httpgd")

# %%
